faApp.controller('SimuOrderListCtrl',['$scope','simuOrderService','$location','$controller','$route','orderManagerService','sessionService','authService',function($scope,simuOrderService,$location,$controller,$route,orderManagerService,sessionService,authService) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.list = [];
  $scope.loading = true;
	$scope.$watch('config',function(){
    simuOrderService.list($scope.config).then(function(json){
      $scope.list = json.list;
      $scope.pager = json.page;
      $scope.loading = false;
    
      angular.forEach($scope.list,function(o,i){
        $scope.list[i]['faInfo']['faShowName'] = $scope.list[i].faInfo.uName || $scope.list[i].faInfo.uRealName || $scope.list[i].faInfo.uLogin;
      });
    });
  },true)
 
	$scope.config = {
		"pageSize" : 20,
		"pageNo" : 1
	};
  
	$scope.pageChanged = function(){
		$scope.config.pageNo = $scope.pager.pageNo;
	};   

  $scope.searchMap = {
		"jjmc" : '', 
  };

  if($scope.orderManagerUserList == undefined){
    var access = sessionService.getItem("user.access","");
    var tmp = "";
    if (access == "all"){
       tmp = "in";
    }else{
      angular.forEach(access.split(","),function(o){
        
        if(o.indexOf("queryOrderManagerUsersList.shtml") == 0){
          tmp = "in";
        }
      });
    }

    if(tmp != undefined && tmp.length){
      orderManagerService.list({"pageSize":9999,"pageNo":1}).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          $scope.orderManagerUserList = json.list;
          $scope.orderManagerUserList.unshift({userId:"isnoManagerUser",username:"无跟进人订单"});
        }
      });
    }
  }

  $scope.isShowOrder = false;
  $scope.orderList = [];
  $scope.fit={};

  simuOrderService.queryOrderList($scope.fit).then(function(json){
    $scope.orderList=json.list || [];
  });

     //点击事件显示消息
$scope.changeShow=function(){
    $scope.isShowOrder = true;
 

}
//显示样式改变
$("#changecss").mouseover(function(){
$("#changecss").removeClass("markyanse");
  $("#changecss").addClass("changeCSS");
});
$("#changecss").mouseleave(function(){
$("#changecss").addClass("markyanse");
  $("#changecss").removeClass("changeCSS");
});




  $scope.search = function(){
    $scope.searchMap.isnoManagerUser = "";
    if($scope.searchMap.systemUserId != undefined && $scope.searchMap.systemUserId == "isnoManagerUser"){
      $scope.searchMap.isnoManagerUser = "yes";
    }
    angular.forEach($scope.searchMap,function(o,i){
      $scope.config[i] = ( undefined == $scope.searchMap[i] ? "" : $scope.searchMap[i]);
    });

  };
  
  $scope.process = function(id){
    simuOrderService.edit({id:id,'status':'02'}).then(function(json){
      if (json.statusCode){
         if(json.statusCode=='2007'){
          alert("订单状态不能逆转!!!!");
          return;
         }else{
           alert("出错了:" + json.statusCode);
         }
      }else{
        $route.reload();
      }
    });
  };

  $scope.chkAllOrderList = function(){
    if($("#chkAllOrderList").attr("checked") == "checked"){
      $("[name='chkOrderList']").attr("checked","checked");
    }else{
      $("[name='chkOrderList']").attr("checked",false);
    }
  };

  $scope.chooseOrderId = "";
  $scope.faids = "";
  $scope.omu = "";
  $scope.chooseOrderIdCount = 0;
  $scope.isShowOrderManagerLayer = false;
  $scope.t = "";
  
  $scope.orderDoOrder = function(t){
    chooseOrderList(t);
    if($scope.chooseOrderId != undefined && $scope.chooseOrderId != ""){
      $scope.chooseOrderIdCount = $scope.chooseOrderId.split(",").length - 1;
      $scope.isShowOrderManagerLayer = true;
    }else{
      alert("请选择订单！");
      return;
    }

    if(t != undefined && t != "" && t == "1"){
      orderManagerService.list({"pageSize":9999,"pageNo":1}).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          $scope.orderManagerUserList2 = json.list;
        }
      });
    }
    $scope.t = t;
  };

  chooseOrderList = function(t){
    $scope.chooseOrderId = "";
    $scope.omu = "";
    $scope.faids = "";
    $("[name='chkOrderList']").each(function(i){
      if($(this).attr("checked") == "checked"){
        if(t == 1 && $(this).attr("omu")){
          alert("请选择没有订单管理员的订单！");
          $scope.chooseOrderId = -1;
          return false;
        }else if(t == 0 && !$(this).attr("omu")){
          alert("请选择有订单管理员的订单！");
          $scope.chooseOrderId = -2;
          return false;
        }
        
        if($(this).attr("value") != undefined && $(this).attr("value") != ""){
          $scope.chooseOrderId += $(this).attr("value") + ",";
          $scope.faids += $(this).attr("faid") + ",";
          $scope.omu += $(this).attr("omu") + ",";
        }
      }
    });
    //return $scope.chooseOrderId;
  };

  $scope.managerOrder = {};
  $scope.fenpeiOrHuishou = function(t){
    $scope.managerOrder.orderIds = $scope.chooseOrderId;
    $scope.managerOrder.ptype = "05";
    $scope.managerOrder.faids = $scope.faids;

    if(t == "1"){
      if($scope.managerOrder.systemUserId != undefined && $scope.managerOrder.systemUserId != "" && t != ""){
        // 分配操作
        $scope.managerOrder.isConfirm = "0";
        orderManagerService.managerOrderUserAdd($scope.managerOrder).then(function(json){
          if (json.statusCode){
            if(json.statusCode == '4001'){

              var tmp = "理财师已被分配给了不同的渠道，请确认操作！\r";
              if(json.mess != undefined){
                angular.forEach(json.mess, function(o){
                  tmp += "理财师" + o.uid + "已被分配给：" + o.name + ";\r";
                });
              }

              if(confirm(tmp || "理财师已被分配给了不同的渠道，请确认操作！")){
                $scope.managerOrder.isConfirm = "1";
                orderManagerService.managerOrderUserAdd($scope.managerOrder).then(function(json){
                  if (json.statusCode){
                    alert("出错了:" + json.statusCode);
                  }else{
                    // 操作成功！分配成功！
                    $scope.alertNextPage("分配成功！");
                    $route.reload();
                  }
                });
              }
            }else{
              alert("出错了:" + json.statusCode);
            }
          }else{
            // 操作成功！分配成功！
            $scope.alertNextPage("分配成功！");
            $route.reload();
          }
        },function(res){
          alert('网络状态不稳定或超时！');
        });
      }else{
        alert("请选择订单管理员！");
      }
    }else if(t == '0'){
      var tmpid = "";
      angular.forEach($scope.omu.split(","),function(o,i){
        if(i == 0){
          tmpid = o;
        }else{
          if(o != undefined && o != "" && o != tmpid){
            tmpid = "99";
          }
        }
      });

      if(tmpid != undefined && tmpid != "99"){
        // 回收时，订单跟进人设为空
        $scope.managerOrder.systemUserId = "";
        // 回收
        orderManagerService.managerOrderUserUpdate($scope.managerOrder).then(function(json){
          if (json.statusCode){
            alert("出错了:" + json.statusCode);
          }else{
            $scope.alertNextPage("回收成功！");
            $route.reload();
          }
        });
      }else{
        if(confirm("选择了多笔跟进人不同的订单，确认要同时回收吗！")){
          // 回收时，订单跟进人设为空
          $scope.managerOrder.systemUserId = "";
          // 回收
          orderManagerService.managerOrderUserUpdate($scope.managerOrder).then(function(json){
            if (json.statusCode){
              alert("出错了:" + json.statusCode);
            }else{
              $scope.alertNextPage("回收成功！");
              $route.reload();
            }
          });
        }
      }
    }
  };
  
}]); 

faApp.controller('SimuOrderViewCtrl',['$scope','simuOrderService','$location','$controller','$route','$routeParams',function($scope,simuOrderService,$location,$controller,$route,$routeParams) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.id = $routeParams.id;
  $scope.item = {};
  $scope.loading = true;
  $scope.review = {'status':'02','id':$scope.id};

  simuOrderService.query({id:$scope.id}).then(function(json){
    if (json.list.length){
      $scope.item = json.list[0] || {};
      $scope.item['faInfo']['faShowName'] = $scope.item.faInfo.uName || $scope.item.faInfo.uRealName || $scope.item.faInfo.uLogin;
    }
    $scope.loading = false;
    
  });
  
  $scope.save = function(id){
    simuOrderService.edit($scope.review).then(function(json){
      if (json.statusCode){
        if(json.statusCode=='2006'){
          alert("还有附件未通过审核!!!!");
          return;
        }else if(json.statusCode=='2007'){
          alert("订单状态不能逆转!!!!");
          return;
        }else{
           alert("出错了:" + json.statusCode);
        }
      }else{
        alert("操作成功");
        $location.path("/simu/order/list");
      }
    });

    // $location.path("/simu/order/list");
  };
 
}]);
